/*
	The MIT License

	Copyright (c) 2010 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

#include "im_local.h"


//
//	EImage::EImage
//
EImage::EImage( uint w, uint h, const EColor &color ) : width(w), height(h), image_data(w*h, color)
{
	
}


//
//	EImage::~EImage
//
EImage::~EImage( void )
{
	image_data.clear();
}


//
//	EImage::Sample
//
EColor EImage::Sample( int x, int y, bool wrap ) const
{
	if (!wrap) {
		if (x < 0) {
			x = 0;
		} else if (x >= (int)width) {
			x = width - 1;
		}
		if (y < 0) {
			y = 0;
		} else if (y >= (int)height) {
			y = height - 1;
		}
	} else {
		if (x < 0) {
			x = width + x%width;
		} 
		if (x >= (int)width) {
			x = x%width;
		}
		if (y < 0) {
			y = height + y%height;
		}
		if (y >= (int)height) {
			y = y%height;
		}
	}

	return image_data[y*width + x];
}


//
//	EImage::Sample
//
EColor EImage::Sample( float x, float y, bool wrap ) const
{
	x -= 0.5f;
	y -= 0.5f;

	float dx = x - floorf(x);	
	float dy = y - floorf(y);	
	
	int ix = (int)floorf(x);
	int iy = (int)floorf(y);
	
	EColor s00	= Sample(ix+0, iy+0, wrap);
	EColor s01	= Sample(ix+0, iy+1, wrap);
	EColor s11	= Sample(ix+1, iy+1, wrap);
	EColor s10	= Sample(ix+1, iy+0, wrap);

	EColor s0x	= EMath::Lerp(s00, s01, dy);	
	EColor s1x	= EMath::Lerp(s10, s11, dy);	
	
	EColor s	= EMath::Lerp(s0x, s1x, dx);

	return s;
}


//
//	EImage::WritePixel
//
void EImage::WritePixel( int x, int y, const EColor &color )
{
	ASSERT(x < (int)width && y < (int)height);
	image_data[y*width + x] = color;
}


//
//	EImage::GetByteRGBABlob
//
IPxBlob EImage::GetByteRGBABlob( void )
{
	IPxBlob b = CoreCreateBlob(width*height*4);

	byte *ptr = static_cast<byte*>( b->GetBufferPointer() );

	uint i, j;
	for (j = 0, i = 0; i < width*height*4; i += 4, j++)
	{
		const EColor *pixel = &image_data[j];

		ptr[i]		=	pixel->x*255;
		ptr[i+1]	=	pixel->y*255;
		ptr[i+2]	=	pixel->z*255;
		ptr[i+3]	=	pixel->w*255;
	}

	return b;
}


//
//	EImage::GetByteRGBABlob
//
const IPxBlob EImage::GetByteRGBABlob( void )	const
{
	IPxBlob b = CoreCreateBlob(width*height*4);

	byte *ptr = static_cast<byte*>( b->GetBufferPointer() );

	uint i, j;
	for (j = 0, i = 0; i < width*height*4; i += 4, j++)
	{
		const EColor *pixel = &image_data[j];

		ptr[i]		=	pixel->x*255;
		ptr[i+1]	=	pixel->y*255;
		ptr[i+2]	=	pixel->z*255;
		ptr[i+3]	=	pixel->w*255;
	}

	return b;
}


//
//	EImage::CreateImageFromBytes
//
void EImage::CreateImageFromBytes( uint w, uint h, uint bpp, byte* bytes )
{
	if (image_data.size()) {
		image_data.clear();
	}
	
	width  = w;
	height = h;

	switch(bpp) {
		case 24:
		{
			for (uint i = 0; i < height*width*3; i += 3)
			{
				image_data.push_back( EColor( float(bytes[i])/255.0f, float(bytes[i+1])/255.0f, float(bytes[i+2])/255.0f, 1.0f ) );
			}
			break;
		}
		case 32:
		{
			for (uint i = 0; i < height*width*4; i += 4)
			{
				image_data.push_back( EColor( float(bytes[i])/255.0f, float(bytes[i+1])/255.0f, float(bytes[i+2])/255.0f, float(bytes[i+3])/255.0f ) );
			}
			break;
		}
	}
}


//
//	EImage::CreateImageFromTGA
//
void EImage::CreateImageFromTGA( IPxBlob b )
{
	byte* ptr = static_cast<byte*>(b->GetBufferPointer());

	byte id_size	= *(ptr++);
	//LOGF("ID size: %d", id_size);
	byte color_type = *(ptr++);
	if (color_type != 0) {
		RUNTIME_ERROR( "Such color type is not supported" );
	}
	//LOGF("Color type: %d", color_type);
	byte image_type = *(ptr++);
	if (image_type != 2) {
		RUNTIME_ERROR( "Such image type is not supported" );
	}
	//LOGF("Image type: %d", image_type);

	ptr += 5; // Skip color map specification
	ptr += 4; // Skip pos x and y

	uint w = (*(ptr + 1) << 8) + *ptr;
	if (w == 0) {
		RUNTIME_ERROR( "Width = 0" );
	}
	ptr +=2;

	uint h = (*(ptr + 1) << 8) + *ptr;
	if (h == 0) {
		RUNTIME_ERROR( "Height = 0" );
	}
	ptr +=2;

	uint bpp = *(ptr++);
	if (bpp == 0 || (bpp != 32 && bpp != 24)) {
		RUNTIME_ERROR( va("Wrong BPP : $d", bpp) );
	}

	uint pitch		= w*(bpp/8);
	uint img_descr	= *(ptr++); // Image descriptor

	ptr	+= id_size;  // Skip Image ID

	if (img_descr & 0x20)
	{
		for (uint line = 0; line < h/2; line++ )
		{
			for (uint col = 0; col < pitch; col++)
			{
				Swap<byte>(*(ptr + col + line*pitch), *(ptr + col + (h - 1)*pitch - line*pitch));
			}
		}
	}
	
	CreateImageFromBytes(w, h, bpp, ptr);
}
